﻿using _17bangMVC.Models.Article;
using BLL.Entities;
using BLL.Repositories;
using SRV.ServiceInterface;
using SRV.ViewModel.Article;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SRV.ProdService
{
    public class ArticleService : BaseService,IArticleService
    {
        private ArticleRepository articleRepository;
        public ArticleService()
        {
            articleRepository = new ArticleRepository(context);
        }
      
        public SingleModel GetById(int id)
        {
            Article article = articleRepository.Find(id);
            SingleModel model = mapper.Map<SingleModel>(article);
            return model;
        }

        //currentUserId是从cookie取出来的Id
        public int Publish(NewModel model)
        {
            if (GetCurrentUser() == null)
            {
                throw new ArgumentException("作者不能为空");
            }
            Article article = mapper.Map<Article>(model);
            articleRepository.Save(article);
            return article.Id;
        }
        public EditModel GetEdit(int id)
        {
            Article article = articleRepository.Find(id);
            EditModel model = mapper.Map<EditModel>(article);
            return model;
        }

        public int Edit(int id, EditModel model)
        {
            Article article = articleRepository.Find(id);
            mapper.Map<EditModel, Article>(model, article);
            return article.Id;
        }

    }
}
